﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace HHWeb.UserControl
{
    public partial class Paging : System.Web.UI.UserControl
    {
        public StringBuilder PagingControlInfo = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetPaging();
        }
        void GetPaging()
        {
            //判断如果当前页为负数或者零时，NowPage=1
            if (NowPage<=0)
                NowPage = 1;
            //判断如果当前页大于总页数时，NowPage=PageCount
            int PageCount = GetPageCount();
            if (NowPage>PageCount)
                NowPage = PageCount;
            //首页和上一页（）
            if (NowPage != 1)
            {
                PagingControlInfo.Append("<a href=\"?NowPage=1\">首页</a>");
                PagingControlInfo.Append("<a href=\"?NowPage=" + (NowPage - 1) + "\">上一页</a>");
            }
            //如果分页总页数小于页码数（PageNumber）时，不显示省略号(...)
            if (PageCount <= PageNumber)
            {
                for (int i = 1; i <= PageCount; i++)
                    GetPageNumberStr(i);
            }
            else
            {
                if (PageNumber % 2 != 0)
                {
                    LeftPageNumberOne = (PageNumber / 2) + 1;
                    LeftPageNumberTwo = (PageNumber / 2);
                    RightPageNumber = (PageNumber / 2);
                }
                else
                {
                    LeftPageNumberOne = (PageNumber / 2) + 1;
                    LeftPageNumberTwo = (PageNumber / 2);
                    RightPageNumber = (PageNumber / 2) - 1;
                }
                //总页数大于页码数（PageNumber）时（简单处理--算法）
                if (NowPage > LeftPageNumberOne && NowPage < PageCount - RightPageNumber)
                {
                    PagingControlInfo.Append("<span>...</span>");
                    for (int i = NowPage - LeftPageNumberTwo; i <= NowPage + RightPageNumber; i++)
                        GetPageNumberStr(i);
                    PagingControlInfo.Append("<span>...</span>");
                }
                else if (NowPage >= PageCount - RightPageNumber)
                {
                    PagingControlInfo.Append("<span>...</span>");
                    for (int i = PageCount - PageNumber; i <= PageCount; i++)
                        GetPageNumberStr(i);
                }
                else
                {
                    for (int i = 1; i <= PageNumber; i++)
                        GetPageNumberStr(i);
                    PagingControlInfo.Append("<span>...</span>");
                }
            }
            //下一页和末页
            if (NowPage!=PageCount)
            {
                PagingControlInfo.Append("<a href=\"?NowPage=" + (NowPage + 1) + "\">下一页</a>");
                PagingControlInfo.Append("<a href=\"?NowPage=" + PageCount + "\">末页</a>");
            }
        }

        #region //拼接页码方法
        public void GetPageNumberStr(int i)
        {
            if (i == NowPage)
                PagingControlInfo.Append("<a href=\"?NowPage="+i+"\" style=\"border:0px;color:#ff5a00 ;\">" + i + "</a>");
            else
                PagingControlInfo.Append("<a href=\"?NowPage=" + i + "\">" + i + "</a>");
        }
        #endregion

        #region //计算总页数
        public int GetPageCount()
        {
            if (Totalcount % PageSize == 0)
                return (Totalcount / PageSize);
            else
                return (Totalcount / PageSize) + 1;
        }
        #endregion

        #region //分页必须提供的基本属性
        /// <summary>
        /// 当前页
        /// </summary>
        public int NowPage { get; set; }
        /// <summary>
        /// 每页显示的记录条数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int Totalcount { get; set; }
        /// <summary>
        /// 页码（意思是前台分页控件显示的页数）
        /// </summary>
        public int PageNumber { get; set; }
        #endregion

        #region //分页基本属性
        /// <summary>
        /// 左页码属性一
        /// </summary>
        public int LeftPageNumberOne { get; set; }
        /// <summary>
        /// 左页码属性二
        /// </summary>
        public int LeftPageNumberTwo { get; set; }
        /// <summary>
        /// 右页码
        /// </summary>
        public int RightPageNumber { get; set; }
         #endregion
    }
}